package com.sinoeyes.controller.compent;

import com.sinoeyes.common.constant.Constants;
import com.sinoeyes.common.core.controller.BaseController;
import com.sinoeyes.common.result.R;
import com.sinoeyes.common.result.RCode;
import com.sinoeyes.common.utils.bean.ObjectUtils;
import com.sinoeyes.compent.po.CurrencyModel;
import com.sinoeyes.compent.service.ICurrencyModelService;
import com.sinoeyes.framework.aspectj.lang.annotation.Log;
import com.sinoeyes.framework.aspectj.lang.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @program: PerformanceX
 * @description: 模板信息接口
 * @author aries.hao
 * @create:  2019/09/20 17:42
 */
@Api(value = "Template API", tags = {"模板信息接口"})
@RestController
@Slf4j
@RequestMapping("/product")
public class ITemplateController extends BaseController {

    @Autowired
    private final ICurrencyModelService modelService;

    @Autowired
    public ITemplateController(ICurrencyModelService modelService) {
        this.modelService = modelService;
    }

    @ApiOperation("获取模板信息（明细）")
    @ApiImplicitParam(name = "templateId", value = "模板ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/getTemplateInfoById/{templateId}")
    public String getTemplateInfoById(@PathVariable String templateId) {
        CurrencyModel currencyModel = modelService.selectCurrencyModelById(templateId);
        if (ObjectUtils.isNotEmpty(currencyModel)) {
            return convertJsonWithDate(R.success(currencyModel));
        }
        return jsonResult(R.failure("未获取到此模板相关信息"));
    }

    @ApiOperation("条件获取模板信息")
    @PostMapping("/getTemplateList")
    public R getTemplateList(@RequestBody(required = false) CurrencyModel currencyModel) {
//        startPage();
//        List<CurrencyModel> productInfoList = modelService.selectCurrencyModelList(currencyModel);
//        if (!CollectionUtils.isEmpty(productInfoList)) {
//            return R.success(getRestTable(productInfoList));
//        }
        return R.failure("未获取到模板信息！");
    }

    @ApiOperation("添加模板信息")
    @Log(title = "模板管理", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addTemplate")
    public String addTemplate(@RequestBody CurrencyModel currencyModel) {
        log.info("###########模板信息：" + currencyModel.toString());
        int i = modelService.insertCurrencyModel(currencyModel);
        if (i > 0) {
            return jsonResult(R.success("模板信息添加成功"));
        }
        return jsonResult(R.failure("模板信息添加失败"));
    }

    @ApiOperation("更新模板信息")
    @Log(title = "模板管理", businessType = BusinessType.UPDATE)
    @ApiImplicitParam(name = "productInfo", value = "修改模板信息", dataType = "ProductInfo")
    @PostMapping("/updateTemplate")
    public String updateTemplate(@RequestBody CurrencyModel currencyModel) {
        log.info("###########模板信息：" + currencyModel.toString());
        int i = modelService.updateCurrencyModel(currencyModel);
        if (i > 0) {
            return jsonResult(R.success("模板信息修改成功"));
        }
        return jsonResult(R.failure("模板信息修改失败"));
    }

    @ApiOperation("删除模板信息")
    @Log(title = "模板管理", businessType = BusinessType.OTHER)
    @ApiImplicitParam(name = "productId", value = "维度ID", required = true, dataType = "String", paramType = "path")
    @DeleteMapping("/deleteTemplate/{templateId}")
    public String deleteTemplate(@PathVariable String templateId) {
        RCode rCode = modelService.deleteCurrencyModelByIds(templateId, Constants.DEL_FLAG);
        if (rCode.getOperateCount() > 0) {
            return jsonResult(R.success("模板信息删除成功"));
        }
        return jsonResult(R.failure("模板信息删除失败[" + rCode.getMessage() + "]"));
    }

    @ApiOperation("启用模板信息")
    @ApiImplicitParam(name = "productId", value = "模板ID", required = true, dataType = "String", paramType = "path")
    @PutMapping("/enableTemplate/{templateId}")
    public String enableTemplate(@PathVariable String templateId) {
        RCode rCode = modelService.deleteCurrencyModelByIds(templateId, Constants.ENABlE_FLAG);
        if (rCode.getOperateCount() > 0) {
            return jsonResult(R.success("模板信息启用成功"));
        }
        return jsonResult(R.failure("模板信息启用失败[" + rCode.getMessage() + "]"));
    }
}
